import Event from './Event';
import EventEmitter from './EventEmitter';
import Page from './Page';

import strategy from '../strategy';
import Node from './Node';

type AppEvent = {
  click: any[];
  mousemove: any[];
  dataChange: any[];
  fetchComplete: any[];
};

class AppCore extends EventEmitter<AppEvent> {
  config: any;
  page?: Page;
  event?: Event;

  constructor(config: any) {
    super();
    console.log(config);
    this.config = config;
    this.initPage();
    this.bindEvent();
  }

  initPage() {
    this.page = new Page(this.config);
  }

  bindEvent() {
    this.event = new Event();
    this.event.init();
    this.on('click', (nodes: Node[]) => strategy.click.triggerEvent(nodes));
    this.on('mousemove', (nodes: Node[]) =>
      strategy.mousemove.triggerEvent(nodes, this.page?.nodeMap)
    );
    this.on('dataChange', (data: Record<string, any>) =>
      strategy.dataChange.triggerEvent(data, this.page?.nodeMap)
    );
  }

  dispose() {
    this.page = undefined;
  }
}

export default AppCore;
